package com.zygh.interactiveplatform.module.service;

import com.zygh.interactiveplatform.module.entity.vo.CpuVO;
import com.zygh.interactiveplatform.module.entity.vo.DiskVO;
import com.zygh.interactiveplatform.module.entity.vo.MemoryVO;
import com.zygh.interactiveplatform.module.entity.vo.MonitorVO;
import lombok.extern.slf4j.Slf4j;
import org.hyperic.sigar.*;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CpuService {

    public MonitorVO info() throws Exception {
        MonitorVO monitorVO = new MonitorVO();
        Sigar sigar = new Sigar();
        monitorVO.setCpu(cpuPerc(sigar));
        monitorVO.setMemory(memory(sigar));
        monitorVO.setDisk(file(sigar));
        // 网络流量：查询最近
//        List<NetVO> list = redisTemplate.opsForList().range(10, 10, (10 - 1));
//        monitorVO.setNetList(list);
        try {
            sigar.close();
        } catch (Exception e) {
            log.error("sigar close失败" + e.getMessage(), e);
        }

        return monitorVO;
    }

    /**
     * 内存
     */
    private MemoryVO memory(Sigar sigar) throws SigarException {
        MemoryVO memoryVO = new MemoryVO();
        // 使用sigar获取内存
        Mem mem = sigar.getMem();
        double memTotal = mem.getTotal();
        double memRam = mem.getRam();
        double memUsed = mem.getActualUsed();
        double memFree = mem.getActualFree();
        double memUsedPerc = mem.getUsedPercent();
        memoryVO.setMemory(String.format("%.2f", memTotal / 1024 / 1024 / 1024));
        memoryVO.setMemRam(String.format("%.2f", memRam / 1024));
        memoryVO.setMemUsed(String.format("%.2f", memUsed / 1024 / 1024 / 1024));
        memoryVO.setMemFrees(String.format("%.2f", memFree / 1024 / 1024 / 1024));
        memoryVO.setMemoryUsage(String.format("%.2f", memUsedPerc));
        return memoryVO;
    }

    /**
     * cpu使用率
     */
    private static CpuVO cpuPerc(Sigar sigar) throws SigarException {
        CpuPerc cpu = sigar.getCpuPerc();
        CpuVO cpuVO = new CpuVO();
//        cpuVO.setUser(CpuPerc.format(cpu.getUser()));
//        cpuVO.setSys(CpuPerc.format(cpu.getSys()));
//        cpuVO.setWait(CpuPerc.format(cpu.getWait()));
//        cpuVO.setNice(CpuPerc.format(cpu.getNice()));
//        cpuVO.setIdle(CpuPerc.format(cpu.getIdle()));
//        cpuVO.setCombined(CpuPerc.format(cpu.getCombined()));
        cpuVO.setUser(String.format("%.1f",cpu.getUser()*100));
        cpuVO.setSys(String.format("%.1f",cpu.getSys()*100));
        cpuVO.setWait(String.format("%.1f",cpu.getWait()*100));
        cpuVO.setNice(String.format("%.1f",cpu.getNice()*100));
        cpuVO.setIdle(String.format("%.1f",cpu.getIdle()*100));
        cpuVO.setCombined(String.format("%.1f",cpu.getCombined()*100));
        return cpuVO;
    }

    private DiskVO file(Sigar sigar) throws Exception {
        DiskVO diskVO = new DiskVO();
        long total = 0L;
        long free = 0L;
        long avail = 0L;
        long used = 0L;
        FileSystem fslist[] = sigar.getFileSystemList();
        for (int i = 0; i < fslist.length; i++) {
            FileSystem fs = fslist[i];
            FileSystemUsage usage = null;
            try {
                usage = sigar.getFileSystemUsage(fs.getDirName());
            } catch (SigarException e) {
                log.info("{}-- 不可用", fs.getDirName());
                continue;
            }
            switch (fs.getType()) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    // 文件系统总大小
                    total += usage.getTotal();
                    // 文件系统剩余大小
                    free += usage.getFree();
                    // 文件系统可用大小
                    avail += usage.getAvail();
                    // 文件系统已经使用量
                    used += usage.getUsed();
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                default:
                    break;
            }
        }
        diskVO.setTotal(String.format("%.2f", total / 1024 / 1024D));
        diskVO.setFree(String.format("%.2f", free / 1024 / 1024D));
        diskVO.setAvail(String.format("%.2f", avail / 1024 / 1024D));
        diskVO.setUsed(String.format("%.2f", used / 1024 / 1024D));
        diskVO.setUsePercent(String.format("%.2f", 100D * used / total));
        return diskVO;
    }
}
